//
//  WelcomeViewController.swift
//  XYWeibo
//
//  Created by 常晓璇 on 6/30/15.
//  Copyright © 2015 chang xiaoxuan. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置头像
        if let urlString = sharedUserAccount?.avatar_large {
            print(urlString)
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { _ in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.messageLabel.alpha = 1.0
                    }, completion: { _ in
                        NSNotificationCenter.defaultCenter().postNotificationName(XYSwitchRootVCNotification, object: true)
                })
        }
        
    }
    
    var bottomConstraint : NSLayoutConstraint?
///  MARK: 设置界面布局
    override func loadView() {
        view = UIView()
        
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        backImageView.ff_Fill(view)
        
        // 记录iconView上添加的约束数组
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        
        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))
    }
    

    
    lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
    
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
    

}
